
/**
 * 页面加载事件
 */
function initData(){
    var arr = JSON.parse(sessionStorage.getItem("userRoleList"));
    for (var item of arr) {
        var span = document.createElement("span");
        span.innerHTML="<input type='checkbox' class='roleId' value='"+item.roleName+"' /> "+item.roleName+" &nbsp;&nbsp;"
        var fu = document.getElementsByClassName("ctx")[0];
        var cz = document.getElementsByClassName("providerAddBtn")[0];
        //渲染节点
        fu.insertBefore(span,cz);
    }
}

/**
 * 根据输入的用户名渲染角色
 */
function showRoleByUserName(){
    //获取用户名
    var userName = document.getElementById("userName").value;
    //遍历匹配
    var roleName;
    var userList = JSON.parse(sessionStorage.getItem("userList"));
    for(var i=0; i<userList.length; i++){
        if(userList[i].yhxm == userName){
            roleName = userList[i].yhlx;
            break;
        }
    }
    var roleList = document.getElementsByClassName("roleId");
    for(var i=0; i<roleList.length; i++){
        roleList[i].checked=false;
        if(roleList[i].value == roleName){
            roleList[i].checked=true;
        }
    }
}

/**
 * 更新保存用户的新角色
 */
function roleSave(){
    //获取勾选的选项
    var roleArr = document.getElementsByClassName("roleId");
    //角色
    var roleName;
    for(var i=0; i<roleArr.length; i++){
        if(roleArr[i].checked){
            roleName = roleArr[i].value;
            break;
        }
    }
    //输入的用户名
    var userName = document.getElementById("userName").value;
    var userList = JSON.parse(sessionStorage.getItem("userList"));
    for(var i=0; i<userList.length; i++){
        if(userList[i].yhxm == userName){
           userList[i].yhlx = roleName;
            break;
        }
    }
    sessionStorage.setItem("userList",JSON.stringify(userList));
}